package com.atlantis.apps.socius.tabs.fragments;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.atlantis.apps.facebook.BaseDialogListener;
import com.atlantis.apps.facebook.BaseRequestListener;
import com.atlantis.apps.facebook.FriendsGetProfilePics;
import com.atlantis.apps.facebook.Utility;
import com.atlantis.apps.socius.R;
import com.facebook.android.FacebookError;

/**
 * @author mayank
 *
 */
public class SNFriendsFragment extends SNFragment implements OnItemClickListener {
    private Handler mHandler;

    protected ListView friendsList;
    protected static JSONArray jsonArray;
    protected String graph_or_fql;
    ProgressDialog dialog;
    ViewGroup vg;

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (container == null) {
            return null;
        }
		
		vg = (ViewGroup)inflater.inflate(R.layout.friends_list, container, false);

		return vg;
	}

    public void onResume() {
        super.onResume();
        mHandler = new Handler();
        
        if (Utility.mFacebook.isSessionValid()) {
            String query = "select name, current_location, uid, pic_square from user where uid in (select uid2 from friend where uid1=me()) order by name";
            Bundle params = new Bundle();
            params.putString("method", "fql.query");
            params.putString("query", query);
            Utility.mAsyncRunner.request(null, params, new FriendsRequestListener());
        } else {
            showToast("Connect with Facebook to see your data");
        }
    	
        Bundle extras = this.getActivity().getIntent().getExtras();
        if (extras != null) {
            String apiResponse = extras.getString("API_RESPONSE");
            graph_or_fql = extras.getString("METHOD");
            try {
                if (graph_or_fql.equals("graph")) {
                    jsonArray = new JSONObject(apiResponse).getJSONArray("data");
                } else {
                    jsonArray = new JSONArray(apiResponse);
                }
            } catch (JSONException e) {
                showToast("Error: " + e.getMessage());
            }
            
            friendsList = (ListView) vg.findViewById(R.id.friends_list);
            friendsList.setOnItemClickListener(this);
            friendsList.setAdapter(new FriendListAdapter(this));
            
            showToast(getString(R.string.can_post_on_wall));
        }
        
        Log.d(getClass().getSimpleName(), getClass().getSimpleName() + " Resumed");
    }	
	
	/*
     * Clicking on a friend should popup a dialog for user to post on friend's
     * wall.
     */
    
    public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
        try {
            final long friendId;
            if (graph_or_fql.equals("graph")) {
                friendId = jsonArray.getJSONObject(position).getLong("id");
            } else {
                friendId = jsonArray.getJSONObject(position).getLong("uid");
            }
            String name = jsonArray.getJSONObject(position).getString("name");

            new AlertDialog.Builder(SNFriendsFragment.this.getActivity()).setTitle(R.string.post_on_wall_title)
                    .setMessage(String.format(getString(R.string.post_on_wall), name))
                    .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                        
                        public void onClick(DialogInterface dialog, int which) {
                            Bundle params = new Bundle();
                            /*
                             * Source Tag: friend_wall_tag To write on a friend's wall, 
                             * provide friend's UID in the 'to' parameter.
                             * More info on feed dialog:
                             * https://developers.facebook.com/docs/reference/dialogs/feed/
                             */
                            params.putString("to", String.valueOf(friendId));
                            params.putString("caption", getString(R.string.app_name));
                            params.putString("description", getString(R.string.app_desc));
                            params.putString("picture", Utility.HACK_ICON_URL);
                            params.putString("name", getString(R.string.app_action));
                            Utility.mFacebook.dialog(SNFriendsFragment.this.getActivity(), "feed", params,
                                    new PostDialogListener());
                        }

                    }).setNegativeButton(R.string.no, null).show();
        } catch (JSONException e) {
            showToast("Error: " + e.getMessage());
        }
    }

    /*
     * Callback after the message has been posted on friend's wall.
     */
    public class PostDialogListener extends BaseDialogListener {
        
        public void onComplete(Bundle values) {
            final String postId = values.getString("post_id");
            if (postId != null) {
                showToast("Message posted on the wall.");
            } else {
                showToast("No message posted on the wall.");
            }
        }
    }

    public void showToast(final String msg) {
        mHandler.post(new Runnable() {
            public void run() {
                Toast toast = Toast.makeText(SNFriendsFragment.this.getActivity(), msg, Toast.LENGTH_LONG);
                toast.show();
            }
        });
    }

    /**
     * Definition of the list adapter
     */
    public class FriendListAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        SNFriendsFragment friendsList;

        public FriendListAdapter(SNFriendsFragment friendsList) {
            this.friendsList = friendsList;
            if (Utility.model == null) {
                Utility.model = new FriendsGetProfilePics();
            }
            Utility.model.setListener(this);
            mInflater = LayoutInflater.from(friendsList.getActivity().getBaseContext());
        }

        
        public int getCount() {
            return jsonArray.length();
        }

        
        public Object getItem(int position) {
            return null;
        }

        
        public long getItemId(int position) {
            return 0;
        }

        
        public View getView(int position, View convertView, ViewGroup parent) {
            JSONObject jsonObject = null;
            try {
                jsonObject = jsonArray.getJSONObject(position);
            } catch (JSONException e1) {
                e1.printStackTrace();
            }
            View hView = convertView;
            if (convertView == null) {
                hView = mInflater.inflate(R.layout.friend_item, null);
                ViewHolder holder = new ViewHolder();
                holder.profile_pic = (ImageView) hView.findViewById(R.id.profile_pic);
                holder.name = (TextView) hView.findViewById(R.id.name);
                holder.info = (TextView) hView.findViewById(R.id.info);
                hView.setTag(holder);
            }

            ViewHolder holder = (ViewHolder) hView.getTag();
            try {
                if (graph_or_fql.equals("graph")) {
                    holder.profile_pic.setImageBitmap(Utility.model.getImage(
                            jsonObject.getString("id"), jsonObject.getString("picture")));
                } else {
                    holder.profile_pic.setImageBitmap(Utility.model.getImage(
                            jsonObject.getString("uid"), jsonObject.getString("pic_square")));
                }
            } catch (JSONException e) {
                holder.name.setText("");
            }
            try {
                holder.name.setText(jsonObject.getString("name"));
            } catch (JSONException e) {
                holder.name.setText("");
            }
            try {
                if (graph_or_fql.equals("graph")) {
                    holder.info.setText(jsonObject.getJSONObject("location").getString("name"));
                } else {
                    JSONObject location = jsonObject.getJSONObject("current_location");
                    holder.info.setText(location.getString("city") + ", "
                            + location.getString("state"));
                }

            } catch (JSONException e) {
                holder.info.setText("");
            }
            return hView;
        }

    }

    class ViewHolder {
        ImageView profile_pic;
        TextView name;
        TextView info;
    }
    /*
     * callback after friends are fetched via me/friends or fql query.
     */
    public class FriendsRequestListener extends BaseRequestListener {

        public void onComplete(final String response, final Object state) {
            if (dialog != null){
            	dialog.dismiss();
            }
/*            Intent myIntent = new Intent(SNFriendsFragment.this., SNFriendsFragment.class);
            myIntent.putExtra("API_RESPONSE", response);
            myIntent.putExtra("METHOD", graph_or_fql);
            startActivity(myIntent);
*/        
            showToast("Friends List loaded");
        }

        public void onFacebookError(FacebookError error) {
            if (dialog != null){
            	dialog.dismiss();
            }
            
            showToast("Facebook Error: " + error.getMessage());
        }
    }
}
